"""
AWS SDK for Java MCP Server

面向软件：aws-sdk-java (AWS SDK for Java)

MCP tools 列表：
1. aws_sdk_java_compile - 编译使用 AWS SDK for Java 的 Java 源文件
   参数：source_file (str) - Java 源文件路径
        sdk_modules (List[str]) - 需要使用的 AWS SDK 模块列表
   返回：统一的 JSON 结构，包含编译结果

2. aws_sdk_java_run - 运行已编译的 AWS SDK for Java 程序
   参数：class_name (str) - 要运行的 Java 类名
        classpath_dirs (List[str]) - 类路径目录列表
        program_args (str) - 程序参数
   返回：统一的 JSON 结构，包含运行结果
"""

from mcp.server.fastmcp import FastMCP, Context
import subprocess
import os
from typing import List

mcp = FastMCP("AWS SDK for Java MCP Server")

@mcp.tool()
def aws_sdk_java_compile(source_file: str, sdk_modules: List[str] = None) -> dict:
    """
    编译使用 AWS SDK for Java 的 Java 源文件
    
    参数:
        source_file: Java 源文件路径
        sdk_modules: 需要使用的 AWS SDK 模块列表，如 ["core", "s3", "kms"]
    
    返回:
        JSON 结构: {
            "success": bool,      # 编译是否成功
            "command": str,       # 执行的完整命令
            "exit_code": int,     # 退出码
            "stdout": str,        # 标准输出
            "stderr": str         # 标准错误
        }
    """
    if sdk_modules is None:
        sdk_modules = ["core", "s3", "kms"]
    
    # 构建类路径
    classpath_parts = ["."]
    for module in sdk_modules:
        classpath_parts.append(f"/usr/share/java/aws-sdk-java/aws-java-sdk-{module}.jar")
    
    classpath = ":".join(classpath_parts)
    
    # 构建编译命令
    command = ["javac", "-cp", classpath, source_file]
    cmd_str = " ".join(command)
    
    try:
        result = subprocess.run(command, capture_output=True, text=True, cwd=os.path.dirname(source_file) or ".")
        
        return {
            "success": result.returncode == 0,
            "command": cmd_str,
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": cmd_str,
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def aws_sdk_java_run(class_name: str, classpath_dirs: List[str] = None, program_args: str = "") -> dict:
    """
    运行已编译的 AWS SDK for Java 程序
    
    参数:
        class_name: 要运行的 Java 类名
        classpath_dirs: 类路径目录列表
        program_args: 程序参数
    
    返回:
        JSON 结构: {
            "success": bool,      # 运行是否成功
            "command": str,       # 执行的完整命令
            "exit_code": int,     # 退出码
            "stdout": str,        # 标准输出
            "stderr": str         # 标准错误
        }
    """
    if classpath_dirs is None:
        classpath_dirs = ["."]
    
    # 构建基础类路径
    base_classpath = [
        ".",
        "/usr/share/java/aws-sdk-java/aws-java-sdk-core.jar",
        "/usr/share/java/aws-sdk-java/aws-java-sdk-s3.jar",
        "/usr/share/java/aws-sdk-java/aws-java-sdk-kms.jar",
        "/usr/share/java/commons-logging.jar",
        "/usr/share/java/jackson-core.jar",
        "/usr/share/java/jackson-databind.jar",
        "/usr/share/java/jackson-annotations.jar",
        "/usr/share/java/httpcomponents/httpclient.jar",
        "/usr/share/java/httpcomponents/httpcore.jar",
        "/usr/share/java/joda-time.jar"
    ]
    
    # 合并类路径
    full_classpath = base_classpath + classpath_dirs
    classpath_str = ":".join(full_classpath)
    
    # 构建运行命令
    command = ["java", "-cp", classpath_str, class_name]
    if program_args:
        command.append(program_args)
    
    cmd_str = " ".join(command)
    
    try:
        result = subprocess.run(command, capture_output=True, text=True)
        
        return {
            "success": result.returncode == 0,
            "command": cmd_str,
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": cmd_str,
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

if __name__ == "__main__":
    mcp.run()